﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.HttpRequest
{
    public class CustomerHttpRequest
    {
        public static String RestPost(String url, Dictionary<String, String> header = null, Dictionary<String, String> parameter = null, String body = "")
        {
            return Request(url, header, parameter, body, Method.POST);
        }
        public static String RestGet(String url, Dictionary<String, String> header = null, Dictionary<String, String> parameter = null)
        {
            return Request(url, header, parameter, String.Empty, Method.GET);
        }
        private static String Request(String url, Dictionary<String, String> header, Dictionary<String, String> parameter, String body, Method method)
        {
            String str = string.Empty;

            RestClient client = new RestClient();
            RestRequest request = null;
            IRestResponse response = null;

            try
            {
                var uri = new Uri(url);
                request = new RestRequest(new Uri(url), method);
                //request.Timeout = 2000;
                request.RequestFormat = DataFormat.Json;
                if (header != null)
                {
                    foreach (var item in header.Keys)
                    {
                        request.AddHeader(item, header[item]);
                    }
                }
                if (parameter != null)
                {
                    foreach (var item in parameter.Keys)
                    {
                        request.AddParameter(item, parameter[item]);
                    }
                }
                if (!String.IsNullOrEmpty(body))
                {
                    request.AddJsonBody(body);
                }
                response = client.Execute(request);
                str = response.Content;
                if (String.IsNullOrEmpty(str))
                {
                    str = response.ErrorMessage;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
            return str;
        }

        //public async static Task<String> Post(String url, Dictionary<String, String> header = null, Dictionary<String, String> parameter = null, String body = "")
        //{
        //    return await Request(url, header, parameter, body, "Post");
        //}
        //public async static Task<String> Get(String url, Dictionary<String, String> header = null, Dictionary<String, String> parameter = null)
        //{
        //    return await Request(url, header, parameter, String.Empty, "GET");
        //}
        //private async static Task<String> Request(String url, Dictionary<String, String> header, Dictionary<String, String> parameter, String body, string method)
        //{
        //    String str = string.Empty;
        //    WebRequest request = null;
        //    Stream streamReques = null;
        //    Stream streamResponse = null;
        //    WebResponse response = null;
        //    try
        //    {
        //        request = HttpWebRequest.Create(new Uri(url));
        //        request.Method = method;
        //        if (header != null)
        //        {
        //            request.Headers.Clear();
        //            foreach (var item in header.Keys)
        //            {
        //                request.Headers.Add(item, header[item]);
        //            }
        //        }
        //        byte[] parameterHeaderBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(parameter));

        //        request.ContentType = "application/json;charset=UTF-8";
        //        request.ContentLength = parameterHeaderBytes.Length;
        //        streamReques = request.GetRequestStream();
        //        streamReques.Write(parameterHeaderBytes);

        //        response = request.GetResponse();
        //        streamResponse = response.GetResponseStream();
        //        byte[] responseBytes = new byte[streamResponse.Length];
        //        streamResponse.Read(responseBytes);

        //        str = Encoding.UTF8.GetString(responseBytes);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        streamReques.Dispose();
        //        streamResponse.Dispose();
        //        response?.Dispose();
        //    }
        //    return str;
        //}
    }

}
